package com.xt.wikiperfumapi.Controller.fragrance;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.page.PageMethod;
import com.xt.wikiperfumapi.model.FragranceNoteInfo;
import com.xt.wikiperfumapi.model.PerfumeViewInfo;
import com.xt.wikiperfumapi.model.vo.FragranceNoteVo;
import com.xt.wikiperfumapi.model.vo.Label;
import com.xt.wikiperfumapi.model.vo.RestBean;
import com.xt.wikiperfumapi.model.wrapper.FragranceNoteWrapper;
import com.xt.wikiperfumapi.model.wrapper.IngredientWrapper;
import com.xt.wikiperfumapi.service.impl.FragranceNoteInfoServiceImpl;
import com.xt.wikiperfumapi.service.impl.PerfumeViewInfoServiceImpl;
import com.xt.wikiperfumapi.util.JwtUtil;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/api/fragrance")
@Slf4j
public class FragranceController {

    @Resource
    FragranceNoteInfoServiceImpl fragranceNoteInfoService;

    @Resource
    FragranceNoteWrapper fragranceNoteWrapper;

    @Resource
    PerfumeViewInfoServiceImpl perfumeViewInfoService;

    @Resource
    JwtUtil jwtUtil;

    @RequestMapping("/queryList")
    public RestBean queryList(){
        List<FragranceNoteInfo> fragranceNoteInfos = fragranceNoteInfoService.list(
                new QueryWrapper<FragranceNoteInfo>()
                .eq("is_deleted", 0)

        );
        List<FragranceNoteVo > fragranceNoteVos=new ArrayList<>();
        fragranceNoteInfos.forEach(el->{
            fragranceNoteVos.add(fragranceNoteWrapper.fragranceNoteToVo(el,6));
        });
        return RestBean.success(fragranceNoteVos);
    }

    @RequestMapping("/get/{id}")
    public RestBean getById(@PathVariable long id, HttpServletRequest request){
        try {
            FragranceNoteInfo fragranceNoteInfo = fragranceNoteInfoService.getById(id);
            PerfumeViewInfo perfumeViewInfo = new PerfumeViewInfo();
            perfumeViewInfo.setPerfumeId(id);
            String uuid = jwtUtil.getJwtUUID(request);
            perfumeViewInfo.setUserId(uuid);
            perfumeViewInfo.setInputTime(new Date());
            perfumeViewInfoService.save(perfumeViewInfo);
            return RestBean.success(fragranceNoteWrapper.fragranceNoteToVo(fragranceNoteInfo,0));
        }catch (Exception e){
            log.info(e.getMessage());
            return RestBean.failure(400,"不存在该香型");
        }
    }

    @RequestMapping("/list")
    public RestBean list(){
        List<FragranceNoteInfo> fragranceNoteInfos = fragranceNoteInfoService.list(new QueryWrapper<FragranceNoteInfo>()
                .eq("is_deleted", 0)
        );
        List<Label> list=new ArrayList<>();
        fragranceNoteInfos.forEach(el->{
            list.add(new Label(el.getName(),el.getFragranceNoteId().toString()));
        });
        return RestBean.success(list);
    }

    @RequestMapping("/update")
    public RestBean update(@RequestBody FragranceNoteInfo fragranceNoteInfo){
        try {
            fragranceNoteInfo.setUpdateTime(new Date());
            return fragranceNoteInfoService.updateById(fragranceNoteInfo)?
                    RestBean.success():RestBean.failure(500,"server error");
        }catch (Exception e){
            return RestBean.failure(400,"不存在该香调信息");
        }
    }

    @RequestMapping("/del/{id}")
    public RestBean del(@PathVariable long id){
        try {
            return fragranceNoteInfoService.removeById(id)?
                    RestBean.success():RestBean.failure(500,"server error");
        }catch (Exception e){
            return RestBean.failure(400,"不存在该香调信息");
        }
    }

    @RequestMapping("/getid/{id}")
    public RestBean getById(@PathVariable long id){
        try {
            return RestBean.success(fragranceNoteInfoService.getById(id));
        }catch (Exception e){
            return RestBean.failure(400,"不存在该香调信息");
        }
    }
}
